Fix --help/--version when a conftest imports Django models - #1285
Open
alimony wants to merge 3 commits into
Open
Fix --help/--version when a conftest imports Django models#1285alimony wants to merge 3 commits into
--help/--version when a conftest imports Django models#1285alimony wants to merge 3 commits into
Conversation
pytest_load_initial_conftests returned early for --help/--version, skipping django.setup(). pytest still imports the initial conftests for these options, so a conftest.py with a top-level Django model import failed with AppRegistryNotReady, surfaced as a "could not load initial conftests" warning (a hard error under filterwarnings = error). Keep setting Django up for --help/--version so those conftests import cleanly. A broken configuration (e.g. an invalid DJANGO_SETTINGS_MODULE) is tolerated only for these options, so they keep working regardless of Django's state, preserving the behavior from pytest-dev#235; a real test run still fails loudly. Fixes pytest-dev#1152. Related to pytest-dev#1275, an earlier fix for the same issue.
alimony
force-pushed
the
fix-help-version
branch
from
July 28, 2026 14:30
dfb5736 to
29fd49f
Compare
Contributor
Author
|
It seems like all PRs are currently blocked by a Django 6.2 incompatibility. I have a fix for it here: #1286 |
Member
|
Please sync master |
Contributor
Author
Done! |
kingbuzzman
reviewed
Jul 28, 2026
Contributor
Author
|
I reworked the try block a little bit. Only two things can actually fail:
The rest (header appends, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Running
pytest --helporpytest --versionfails to load aconftest.pythat imports Django models at import time:pytest.PytestConfigWarning: could not load initial conftests: .../conftest.py
The underlying cause is
AppRegistryNotReady: Apps aren't loaded yet., and it becomes a hard error underfilterwarnings = error. Running the tests themselves works fine, because that path sets Django up first.Cause
pytest_load_initial_conftestsreturned early for--help/--version– added to keep these options working with an invalidDJANGO_SETTINGS_MODULE(#235) – which also skippeddjango.setup(). pytest still imports the initial conftests for these options, so any top-level model import then hitsAppRegistryNotReady.Fix
Continue with normal Django initialization for
--help/--versionso conftests import cleanly. If Django can't be configured or set up (e.g. an invalidDJANGO_SETTINGS_MODULE), the failure is tolerated only for these options, so they keep working regardless of Django's state – preserving #235. A real test run still fails loudly.Tests
Adds a regression test in
tests/test_initialization.pycovering both--helpand--version. It asserts a positive "conftest imported" marker rather than the absence of a warning, so it also covers--version, where pytest records but never prints the conftest-load failure.Notes
This overlaps with #1275, which addresses the same issue. Two differences: the error tolerance here catches any setup failure (not only
ImportError), so a settings module that imports but is improperly configured also won't break--help; and the regression test guards--versionexplicitly.